import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:provider/provider.dart';
import 'package:uikit/uikit.dart';
import 'package:uikit_example/pages/demo/register/view_model/login_view_model.dart';
import 'package:uikit_example/pages/demo/register/view_model/register_view_model.dart';
import 'package:uikit_example/riki_ui_demo.dart';

void main() {
  runMyApp();
}

Future<void> runMyApp() async {
  WidgetsFlutterBinding.ensureInitialized();

  // 捕获Flutter framework异常.
  FlutterError.onError = (FlutterErrorDetails details) async {
    Zone.current.handleUncaughtError(details.exception, details.stack!);
  };

  // 自定义错误页面
  // ErrorWidget.builder = (FlutterErrorDetails flutterErrorDetails) {
  //   //LogUtil.v(flutterErrorDetails.toString());
  //   return const GlobalCodeExceptionPage();
  // };

  await appPlugin.init();

  if (Platform.isAndroid) {
    // 设置Android头部的导航栏透明
    const SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
      statusBarIconBrightness: Brightness.light,
    );
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }

  runApp(MultiProvider(
    providers: <ChangeNotifierProvider<dynamic>>[
      ChangeNotifierProvider<RikiThemeNotifier>(create: (_) => RikiThemeNotifier()),
      ChangeNotifierProvider<RegisterViewModel>(create: (_) => RegisterViewModel()),
      ChangeNotifierProvider<LoginViewModel>(create: (_) => LoginViewModel()),
    ],
    child: const RikiUiKitApp(preview: true),
  ));
}

AppPlugin appPlugin = AppPlugin();

class AppPlugin {
  init() async {
    // 初始化图片缓存器
    ImageUtil.initCacheManager(CacheManager(Config(ImageUtil.key, maxNrOfCacheObjects: 200)));

    // 初始化本地缓存
    await SpUtil.getInstance();
  }
}
//
// Future<void> testRequest() async {
//   // var result = await LoginDao.login("jack", "123456");
//   // print("result === $result");
//   try {
//     var result = await LoginDao.register("jack", "123456", "aa", "bb");
//   } on NeedAuth catch (e) {
//     print("NeedAuth === $e");
//   } on NeedLogin catch (e) {
//     print("NeedLogin === $e");
//   } on HiNetError catch (e) {
//     print("HiNetError === $e");
//   }
// }
